package com.comleader.uap.service;

import static cn.hutool.core.date.DateUtil.formatDateTime;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.UrlLibrarySyncMapper;
import com.comleader.uap.pojo.entity.UrlLibrarySync;
import com.comleader.uap.pojo.query.UrlPushQuery;
import com.comleader.uap.pojo.vo.UrlPushVO;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;

/**
 * @author lianxinjing
 * @description 针对表【NOG_URL_LIBRARY_SYNC(研判网址表)】的数据库操作Service
 * @createDate 2024-11-23 16:23:00
 */
@DS("oracle")
@Service
public class UrlLibrarySyncService extends ServiceImpl<UrlLibrarySyncMapper, UrlLibrarySync> {

    @Resource
    private DictService dictService;

    public List<UrlPushVO> selectList(UrlPushQuery query) {
        // UrlPushQuery urlPushQuery = query.getQueryParams();
        if (CollUtil.isNotEmpty(query.getDiscoveryTime())) {
            query.setDiscoveryStartTimestamp(DateUtil.date(query.getDiscoveryTime().get(0)).getTime());
            query.setDiscoveryEndTimestamp(DateUtil.date(query.getDiscoveryTime().get(1)).getTime());
        }
        List<UrlPushVO> list = baseMapper.selectList(query);
        /*  PageInfo pageInfo = new PageInfo<>(list);
         PageResult<UrlPushVO> pageResult = new PageResult();
         pageResult.setPageNumber(pageInfo.getPageNum());
         pageResult.setPageSize(pageInfo.getPageSize());
         pageResult.setPages(pageInfo.getPages());
         pageResult.setTotal(pageInfo.getTotal());
        List<UrlPushVO> voList = new ArrayList<>();
         list.forEach(data -> {
             UrlPushVO urlPushVO = new UrlPushVO();
             urlPushVO.setUrl(data.getUrl());
             urlPushVO.setType(data.getType());
             urlPushVO.setIsp(data.getIsp());
             urlPushVO.setFraudType(data.getFraudTypeLv1());
             urlPushVO.setFraudTypeDesc(data.getFraudTypeLv1Desc());
             urlPushVO.setSource(data.getIsOverseas());
             urlPushVO.setIcp(data.getIsIcp());
             urlPushVO.setDiscoveryTime(LocalDateTimeUtil.of(data.getFirstDiscoverTime()));
             voList.add(urlPushVO);
         });*/
        // pageResult.setRecords(list);
        return list;
    }

    public void export(UrlPushQuery query, HttpServletResponse response) throws IOException {
        List<Date> discoveryTimeList = query.getDiscoveryTime();
        if (CollUtil.isEmpty(discoveryTimeList) || discoveryTimeList.size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            String fileName = "网址推送_" + formatDateTime(discoveryTimeList.get(0)) + "_" + formatDateTime(discoveryTimeList.get(1)) + ".xlsx";
            List<UrlPushVO> list = selectList(query);
            ExcelUtil.exportMultipleSheet(response, fileName, UrlPushVO.class, "网址推送", list);
        } catch (Exception e) {
            log.error("网址推送导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }

    }

}
